<template>
    <slot v-if="sceneCreated" />
</template>

<script>
/* eslint-disable */
import CocosWrap from '../util/CocosWrap.js'

export default {
  name: "CocosScene",

  props: {
    viewSize:  {
            type: Object,
            default:()=> {return {width:960,height:720}},
        },
    designSize:  {
            type: Object,
            default: ()=>{return {width:2048,height:1536}},
        },
  },

  data() {
    return {
      sceneCreated: false,
    };
  },
  created() {

  },  
  mounted() {
    // cc.view.setFrameSize(this.viewSize.width, this.viewSize.height);
    // cc.view.setDesignResolutionSize(
    //     this.designSize.width,
    //     this.designSize.height,
    //     cc.ResolutionPolicy.EXACT_FIT
    // );

    console.log("scene mounted");

    const scene = new cc.Scene("Scene");
    var canvas = CocosWrap.create({width:this.designSize.width,height:this.designSize.height,name:"Canvas"},[
                    {
                        type: cc.Canvas,
                        props: {
                            designResolution: this.designSize,
                            fitWidth: false,
                            fitHeight: false,
                        },
                    },
                ]);
    canvas.parent = scene;
    this.scene = scene;

    cc.director.runSceneImmediate(scene,
            () => {},
            () => {
              this.ccnode = canvas;
              this.sceneCreated = true;
            }
        );
  },

  // beforeUnmount () {
  //   console.log("scene beforeUnmount");
  // },

  unmounted(){
    console.log("scene unmounted");
    if(this.scene&&cc.director.getScene()===this.scene){
      cc.director.runSceneImmediate(new cc.Scene("TempScene"));
    }
  },

};
</script>

